package com.china08.yxyapi.api.manage.sch.platform.website.image;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.sws.SwsAlbums;
import com.china08.yxyapi.entity.mg.sws.SwsAlbumsImage;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.SwsAlbumsImageRepository;
import com.china08.yxyapi.repository.mg.YxySwsAlbumsRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController("SwsAlbumsCrtController")
public class SwsAlbumsCrtController extends YxyApiBaseController {

    @Autowired
    YxySwsAlbumsRepository yxySwsAlbumsRepository;

    @Autowired
    SwsAlbumsImageRepository swsAlbumsImageRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/sch/platform/website/swsalbums/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String albumId = UUID.randomUUID().toString().replace("-", "");
        String schoolId = getProfile(request, response).getSchoolId();
        String name = reqModel.name;
        String description = reqModel.description;
        String covers = reqModel.covers;
        Integer imageCount = reqModel.imageCount;
        List<String> imageList = reqModel.imageList;

        Assert.isTrue(StringUtils.isNotBlank(name), "相册图片不能为空");

        // 所有相册
        List<SwsAlbums> allList = yxySwsAlbumsRepository.findBySchoolId(schoolId);

        Integer orderNum = 0;

        // 获取数组中的最大值
        for (SwsAlbums swsAlbums : allList) {
            Integer orderInteger = swsAlbums.getOrderNum();
            // 不为空并且比之前的数据大
            if (orderInteger != null && orderInteger > orderNum) {
                orderNum = orderInteger;
            }
        }
        orderNum = orderNum + 1;

        // 创建相册
        SwsAlbums swsAlbums = new SwsAlbums();
        swsAlbums.setId(albumId);
        swsAlbums.setSchoolId(schoolId);
        swsAlbums.setCovers(covers);
        swsAlbums.setName(name);
        swsAlbums.setDescription(description);
        swsAlbums.setImageCount(imageCount);
        swsAlbums.setOrderNum(orderNum);
        swsAlbums.setAlbumsDate(new Date());

        yxySwsAlbumsRepository.save(swsAlbums);

        orderNum = 1;
        // 创建相册图片
        for (String string : imageList) {
            SwsAlbumsImage swsAlbumsImage = new SwsAlbumsImage();
            swsAlbumsImage.setId(UUID.randomUUID().toString().replace("-", ""));
            swsAlbumsImage.setSchoolId(schoolId);
            swsAlbumsImage.setAlbumId(albumId);
            swsAlbumsImage.setImagePath(string);
            swsAlbumsImage.setOrderNum(orderNum);
            swsAlbumsImageRepository.save(swsAlbumsImage);
            orderNum = orderNum + 1;
        }
        return true;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String name;

        public String description;

        public String covers;

        public Integer imageCount;

        public List<String> imageList;
    }

    protected static class RespModel extends YxyApiRespModel {
    }
}
